CMAKE_MINIMUM_REQUIRED(VERSION 3.10)

SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_PROCESSOR cortex-m4)
#SET(CMAKE_VERBOSE_MAKEFILE ON)

SET(CMAKE_EXPORT_COMPILE_COMMANDS ON)

SET(CMAKE_C_COMPILER "/home/pan/complier/gcc-arm-none-eabi/bin/arm-none-eabi-gcc")
SET(CMAKE_ASM_COMPILER "/home/pan/complier/gcc-arm-none-eabi/bin/arm-none-eabi-gcc")
SET(CMAKE_C_FLAGS " -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections -Dgcc -O0 -gdwarf-2 -g")
SET(CMAKE_ASM_FLAGS " -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections -x assembler-with-cpp -Wa,-mimplicit-it=thumb  -gdwarf-2")
SET(CMAKE_C_COMPILER_WORKS TRUE)

SET(CMAKE_CXX_COMPILER "/home/pan/complier/gcc-arm-none-eabi/bin/arm-none-eabi-g++")
SET(CMAKE_CXX_FLAGS " -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections -Dgcc -O0 -gdwarf-2 -g")
SET(CMAKE_CXX_COMPILER_WORKS TRUE)

SET(CMAKE_OBJCOPY "/home/pan/complier/gcc-arm-none-eabi/bin/arm-none-eabi-objcopy")
SET(CMAKE_SIZE "/home/pan/complier/gcc-arm-none-eabi/bin/arm-none-eabi-size")

SET(CMAKE_EXE_LINKER_FLAGS " -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections -Wl,--gc-sections,-Map=rt-thread.map,-cref,-u,Reset_Handler -T ${CMAKE_SOURCE_DIR}/board/linker_scripts/link.lds -Wl,--gc-sections,--print-memory-usage")

SET(CMAKE_C_STANDARD 11)
SET(CMAKE_CXX_STANDARD 17)

PROJECT(rtthread C CXX ASM)

INCLUDE_DIRECTORIES(
	modules/algorithm
	applications
	modules/beep
	modules/BMI088
	modules/can_com
	modules/board_com
	rt-thread/components/drivers/usb/cherryusb/common
	rt-thread/components/drivers/usb/cherryusb/core
	rt-thread/components/drivers/usb/cherryusb/class/cdc
	rt-thread/components/drivers/usb/cherryusb/class/msc
	rt-thread/components/drivers/usb/cherryusb/class/hid
	rt-thread/components/drivers/usb/cherryusb/class/audio
	rt-thread/components/drivers/usb/cherryusb/class/video
	rt-thread/components/drivers/usb/cherryusb/class/wireless
	rt-thread/components/drivers/usb/cherryusb/class/dfu
	rt-thread/components/drivers/usb/cherryusb/class/midi
	rt-thread/components/drivers/usb/cherryusb/class/vendor/net
	rt-thread/components/drivers/usb/cherryusb/class/vendor/serial
	rt-thread/components/drivers/usb/cherryusb/osal
	rt-thread/components/libc/compilers/common/include
	rt-thread/components/libc/compilers/newlib
	modules/MOTOR/DAMIAO
	rt-thread/components/drivers/include
	rt-thread/components/drivers/spi
	modules/MOTOR/DJI
	board
	board/CubeMX_Config/Inc
	libraries/HAL_Drivers/drivers
	libraries/HAL_Drivers/drivers/config
	libraries/HAL_Drivers/drivers/drv_flash
	libraries/HAL_Drivers
	libraries/HAL_Drivers/CMSIS/Include
	modules/DSP
	rt-thread/components/finsh
	modules/IMU
	modules/IST8310
	.
	rt-thread/include
	rt-thread/libcpu/arm/common
	rt-thread/libcpu/arm/cortex-m4
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Inc
	libraries/STM32F4xx_HAL/CMSIS/Device/ST/STM32F4xx/Include
	modules/message
	modules/MOTOR
	modules/offline
	rt-thread/components/libc/posix/io/epoll
	rt-thread/components/libc/posix/io/eventfd
	rt-thread/components/libc/posix/io/poll
	rt-thread/components/libc/posix/ipc
	modules/referee
	modules/RGB
	rt-thread/components/legacy/usb/usbdevice
	modules/REMOTE
	modules/steering
	modules/USB
	rt-thread/components/utilities/ulog
)

ADD_DEFINITIONS(
	-DARM_MATH_CM4
	-DARM_MATH_DSP
	-DARM_MATH_MATRIX_CHECK
	-DARM_MATH_ROUNDING
	-DRT_USING_ARM_LIBC
	-DRT_USING_LIBC
	-DRT_USING_NEWLIBC
	-DSTM32F407xx
	-DUSE_HAL_DRIVER
	-D_POSIX_C_SOURCE=1
	-D__RTTHREAD__
)

# Library source files
SET(RT_ALGORITHM_SOURCES
	modules/algorithm/crc16.c
	modules/algorithm/kalman_filter.c
	modules/algorithm/user_lib.c
	modules/algorithm/controller.c
	modules/algorithm/crc8.c
	modules/algorithm/omni.c
	modules/algorithm/QuaternionEKF.c
)

SET(RT_APPLICATIONS_SOURCES
	applications/robot_cmd.c
	applications/main.c
	applications/shoot.c
	applications/gimbal.c
	applications/chassis.c
)

SET(RT_BEEP_SOURCES
	modules/beep/beep.c
)

SET(RT_BMI088_SOURCES
	modules/BMI088/BMI088.c
)

SET(RT_CANCOMMON_SOURCES
	modules/board_com/board_com.c
	modules/can_com/can_common.c
)

SET(RT_CHERRYUSB_SOURCES
	rt-thread/components/drivers/usb/cherryusb/platform/rtthread/usb_msh.c
	rt-thread/components/drivers/usb/cherryusb/port/dwc2/usb_glue_st.c
	rt-thread/components/drivers/usb/cherryusb/osal/usb_osal_rtthread.c
	rt-thread/components/drivers/usb/cherryusb/platform/rtthread/usb_check.c
	rt-thread/components/drivers/usb/cherryusb/demo/cdc_acm_template.c
	rt-thread/components/drivers/usb/cherryusb/class/cdc/usbd_cdc_acm.c
	rt-thread/components/drivers/usb/cherryusb/port/dwc2/usb_dc_dwc2.c
	rt-thread/components/drivers/usb/cherryusb/core/usbd_core.c
)

SET(RT_COMPILER_SOURCES
	rt-thread/components/libc/compilers/common/cctype.c
	rt-thread/components/libc/compilers/common/cstdlib.c
	rt-thread/components/libc/compilers/common/cstring.c
	rt-thread/components/libc/compilers/common/ctime.c
	rt-thread/components/libc/compilers/common/cunistd.c
	rt-thread/components/libc/compilers/common/cwchar.c
	rt-thread/components/libc/compilers/newlib/syscalls.c
)

SET(RT_DAMIAO_SOURCES
	modules/MOTOR/DAMIAO/damiao.c
)

SET(RT_DEVICEDRIVERS_SOURCES
	rt-thread/components/drivers/can/dev_can.c
	rt-thread/components/drivers/core/device.c
	rt-thread/components/drivers/i2c/dev_i2c_bit_ops.c
	rt-thread/components/drivers/i2c/dev_i2c_core.c
	rt-thread/components/drivers/i2c/dev_i2c_dev.c
	rt-thread/components/drivers/ipc/completion_comm.c
	rt-thread/components/drivers/ipc/completion_up.c
	rt-thread/components/drivers/ipc/condvar.c
	rt-thread/components/drivers/ipc/dataqueue.c
	rt-thread/components/drivers/ipc/pipe.c
	rt-thread/components/drivers/ipc/ringblk_buf.c
	rt-thread/components/drivers/ipc/ringbuffer.c
	rt-thread/components/drivers/ipc/waitqueue.c
	rt-thread/components/drivers/ipc/workqueue.c
	rt-thread/components/drivers/misc/rt_drv_pwm.c
	rt-thread/components/drivers/pin/dev_pin.c
	rt-thread/components/drivers/serial/serial_v2.c
	rt-thread/components/drivers/spi/spi_core.c
	rt-thread/components/drivers/spi/spi_dev.c
)

SET(RT_DJI_SOURCES
	modules/MOTOR/DJI/dji.c
)

SET(RT_DRIVERS_SOURCES
	board/CubeMX_Config/Src/stm32f4xx_hal_msp.c
	board/board.c
	libraries/HAL_Drivers/drivers/drv_can.c
	libraries/HAL_Drivers/drivers/drv_flash/drv_flash_f4.c
	libraries/HAL_Drivers/drivers/drv_gpio.c
	libraries/HAL_Drivers/drivers/drv_hard_i2c.c
	libraries/HAL_Drivers/drivers/drv_pwm.c
	libraries/HAL_Drivers/drivers/drv_spi.c
	libraries/HAL_Drivers/drivers/drv_tim.c
	libraries/HAL_Drivers/drivers/drv_usart_v2.c
	libraries/HAL_Drivers/drv_common.c
	libraries/STM32F4xx_HAL/CMSIS/Device/ST/STM32F4xx/Source/Templates/gcc/startup_stm32f407xx.s
)

SET(RT_DSP_SOURCES
)

SET(RT_FINSH_SOURCES
	rt-thread/components/finsh/shell.c
	rt-thread/components/finsh/msh_parse.c
	rt-thread/components/finsh/cmd.c
	rt-thread/components/finsh/msh.c
)

SET(RT_IMU_SOURCES
	modules/IMU/imu_task.c
	modules/IMU/imu.c
)

SET(RT_IST8310_SOURCES
	modules/IST8310/IST8310.c
)

SET(RT_KERNEL_SOURCES
	rt-thread/src/clock.c
	rt-thread/src/components.c
	rt-thread/src/cpu_up.c
	rt-thread/src/idle.c
	rt-thread/src/ipc.c
	rt-thread/src/irq.c
	rt-thread/src/klibc/kstdio.c
	rt-thread/src/klibc/kstring.c
	rt-thread/src/kservice.c
	rt-thread/src/mem.c
	rt-thread/src/mempool.c
	rt-thread/src/object.c
	rt-thread/src/scheduler_comm.c
	rt-thread/src/scheduler_up.c
	rt-thread/src/thread.c
	rt-thread/src/timer.c
)

SET(RT_LIBCPU_SOURCES
	rt-thread/libcpu/arm/common/atomic_arm.c
	rt-thread/libcpu/arm/common/div0.c
	rt-thread/libcpu/arm/common/showmem.c
	rt-thread/libcpu/arm/cortex-m4/context_gcc.S
	rt-thread/libcpu/arm/cortex-m4/cpuport.c
)

SET(RT_LIBRARIES_SOURCES
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_usart.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd_ex.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c
	libraries/STM32F4xx_HAL/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hcd.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_lptim.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_qspi.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cec.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp_ex.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pccard.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c
	libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd.c
)

SET(RT_MESSAGE_SOURCES
	modules/message/message.c
)

SET(RT_MOTOR_SOURCES
	modules/MOTOR/motor_task.c
)

SET(RT_OFFLINE_SOURCES
	modules/offline/offline.c
)

SET(RT_POSIX_SOURCES
)

SET(RT_REFEREE_SOURCES
	modules/referee/referee_task.c
	modules/referee/referee_ui.c
	modules/referee/referee_crc.c
	modules/referee/referee.c
)

SET(RT_RGB_SOURCES
	modules/RGB/RGB.c
)

SET(RT_RT_KPRINTF_SOURCES
	packages/rt_vsnprintf_full-latest/rt_vsnprintf.c
)

SET(RT_RT_USBD_SOURCES
	rt-thread/components/legacy/usb/usbdevice/core/usbdevice_core.c
	rt-thread/components/legacy/usb/usbdevice/core/usbdevice.c
)

SET(RT_SBUS_SOURCES
	modules/REMOTE/sbus.c
	modules/REMOTE/sbus_task.c
)

SET(RT_STEERING_SOURCES
	modules/steering/steering.c
)

SET(RT_USB_SOURCES
	modules/USB/usb.c
)

SET(RT_UTILITIES_SOURCES
	rt-thread/components/utilities/ulog/backend/console_be.c
	rt-thread/components/utilities/ulog/ulog.c
)

# Library search paths
SET(RT_DSP_LINK_DIRS
	/home/pan/vswork/robomaster/newrm/modules/DSP
)

# Library local macro definitions
SET(RT_DEVICEDRIVERS_DEFINES
	__RT_IPC_SOURCE__
)

SET(RT_KERNEL_DEFINES
	__RT_KERNEL_SOURCE__
)

# Library dependencies
SET(RT_COMPILER_LIBS
	c
	m
)

SET(RT_DSP_LIBS
	arm_cortexM4lf_math.a
)

# Libraries
ADD_LIBRARY(rtt_algorithm OBJECT ${RT_ALGORITHM_SOURCES})
ADD_LIBRARY(rtt_beep OBJECT ${RT_BEEP_SOURCES})
ADD_LIBRARY(rtt_BMI088 OBJECT ${RT_BMI088_SOURCES})
ADD_LIBRARY(rtt_cancommon OBJECT ${RT_CANCOMMON_SOURCES})
ADD_LIBRARY(rtt_CherryUSB OBJECT ${RT_CHERRYUSB_SOURCES})
ADD_LIBRARY(rtt_Compiler OBJECT ${RT_COMPILER_SOURCES})
ADD_LIBRARY(rtt_damiao OBJECT ${RT_DAMIAO_SOURCES})
ADD_LIBRARY(rtt_DeviceDrivers OBJECT ${RT_DEVICEDRIVERS_SOURCES})
ADD_LIBRARY(rtt_dji OBJECT ${RT_DJI_SOURCES})
ADD_LIBRARY(rtt_Drivers OBJECT ${RT_DRIVERS_SOURCES})
ADD_LIBRARY(rtt_Finsh OBJECT ${RT_FINSH_SOURCES})
ADD_LIBRARY(rtt_imu OBJECT ${RT_IMU_SOURCES})
ADD_LIBRARY(rtt_IST8310 OBJECT ${RT_IST8310_SOURCES})
ADD_LIBRARY(rtt_Kernel OBJECT ${RT_KERNEL_SOURCES})
ADD_LIBRARY(rtt_libcpu OBJECT ${RT_LIBCPU_SOURCES})
ADD_LIBRARY(rtt_Libraries OBJECT ${RT_LIBRARIES_SOURCES})
ADD_LIBRARY(rtt_message OBJECT ${RT_MESSAGE_SOURCES})
ADD_LIBRARY(rtt_motor OBJECT ${RT_MOTOR_SOURCES})
ADD_LIBRARY(rtt_offline OBJECT ${RT_OFFLINE_SOURCES})
ADD_LIBRARY(rtt_referee OBJECT ${RT_REFEREE_SOURCES})
ADD_LIBRARY(rtt_RGB OBJECT ${RT_RGB_SOURCES})
ADD_LIBRARY(rtt_rt_kprintf OBJECT ${RT_RT_KPRINTF_SOURCES})
ADD_LIBRARY(rtt_rt_usbd OBJECT ${RT_RT_USBD_SOURCES})
ADD_LIBRARY(rtt_sbus OBJECT ${RT_SBUS_SOURCES})
ADD_LIBRARY(rtt_steering OBJECT ${RT_STEERING_SOURCES})
ADD_LIBRARY(rtt_usb OBJECT ${RT_USB_SOURCES})
ADD_LIBRARY(rtt_Utilities OBJECT ${RT_UTILITIES_SOURCES})

# Interface libraries
ADD_LIBRARY(rtt_DSP INTERFACE)
ADD_LIBRARY(rtt_POSIX INTERFACE)

# Private macros
TARGET_COMPILE_DEFINITIONS(rtt_DeviceDrivers PRIVATE ${RT_DEVICEDRIVERS_DEFINES})
TARGET_COMPILE_DEFINITIONS(rtt_Kernel PRIVATE ${RT_KERNEL_DEFINES})

# Interface library search paths
TARGET_LINK_LIBRARIES(rtt_Compiler INTERFACE ${RT_COMPILER_LIBS})
TARGET_LINK_DIRECTORIES(rtt_DSP INTERFACE ${RT_DSP_LINK_DIRS})
TARGET_LINK_LIBRARIES(rtt_DSP INTERFACE ${RT_DSP_LIBS})

ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}.elf ${RT_APPLICATIONS_SOURCES})
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}.elf
	rtt_algorithm
	rtt_beep
	rtt_BMI088
	rtt_cancommon
	rtt_CherryUSB
	rtt_Compiler
	rtt_damiao
	rtt_DeviceDrivers
	rtt_dji
	rtt_Drivers
	rtt_Finsh
	rtt_imu
	rtt_IST8310
	rtt_Kernel
	rtt_libcpu
	rtt_Libraries
	rtt_message
	rtt_motor
	rtt_offline
	rtt_referee
	rtt_RGB
	rtt_rt_kprintf
	rtt_rt_usbd
	rtt_sbus
	rtt_steering
	rtt_usb
	rtt_Utilities
	rtt_DSP
	rtt_POSIX
)

ADD_CUSTOM_COMMAND(TARGET ${CMAKE_PROJECT_NAME}.elf POST_BUILD 
	COMMAND ${CMAKE_OBJCOPY} -O binary ${CMAKE_PROJECT_NAME}.elf rtthread.bin
	COMMAND ${CMAKE_SIZE} ${CMAKE_PROJECT_NAME}.elf
)

# if custom.cmake is exist, add it
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/custom.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/custom.cmake)
endif()
